//
//  JBWEmoticonTools.swift
//  新浪微博
//
//  Created by 季伯文 on 2017/7/11.
//  Copyright © 2017年 bowen. All rights reserved.
//

import UIKit

//最大列数
 let JBWEMOTICONMAXCOL = 7
//最大行数
 let JBWEMOTICONMAXROW = 3
//最大个数
 let JBWEMOTICONMAXCOUNT = (JBWEMOTICONMAXCOL*JBWEMOTICONMAXROW) - 1

class JBWEmoticonTools: NSObject {

    //全局访问点
    static let share: JBWEmoticonTools = JBWEmoticonTools()
    
    //保存路径
    let file = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last! as NSString).appendingPathComponent("recent.archiver")
    
    //表情键盘bundle对象
    lazy var emoticonBundle: Bundle = {
        //获取路径
        let path = Bundle.main.path(forResource: "Emoticons.bundle", ofType: nil)!
        let bundle = Bundle(path: path)!
        return bundle
        
    }()
    
    //最近
    lazy var recentEmoticons:[JBWEmoticonModel] = {
        return self.getRecentEmoticon()
    }()
    //默认
    lazy var normalEmoticons:[JBWEmoticonModel] = {
        return self.loadEmoticon(fileName: "default")
    }()
    //emoji
    lazy var emojiEmoticons:[JBWEmoticonModel] = {
        return self.loadEmoticon(fileName: "emoji")
    }()
    //lxh
    lazy var lxhEmoticons:[JBWEmoticonModel] = {
        return self.loadEmoticon(fileName: "lxh")
    }()
    
    //三维数组
    lazy var allEmoticons: [[[JBWEmoticonModel]]] = {
        return [
            [self.recentEmoticons],
            self.loadEmoticonGroup(emoticons: self.normalEmoticons),
            self.loadEmoticonGroup(emoticons: self.emojiEmoticons),
            self.loadEmoticonGroup(emoticons: self.lxhEmoticons)]

        
        
    }()

}

//通过路径获取不同的模型一维数组
extension JBWEmoticonTools{
    fileprivate func loadEmoticon(fileName: String) -> [JBWEmoticonModel]{
        
        //路径
        let file = emoticonBundle.path(forResource: "\(fileName)/info.plist", ofType: nil)!
        //字典数组
        let plistArray = NSArray(contentsOfFile: file)!
        //使用yymodel
        let tempList = NSArray.yy_modelArray(with: JBWEmoticonModel.self, json: plistArray) as! [JBWEmoticonModel]
        //遍历tempList
        //得到每个图片的全路径
        for model in tempList {
            model.allPath = "\(fileName)/\(model.png ?? "")"
        }
        return tempList
        
    }
    
    func loadEmoticonGroup(emoticons: [JBWEmoticonModel]) -> [[JBWEmoticonModel]] {
        
        //计算传入的一维数组应该显示多少页
        let pageCount = (emoticons.count + (JBWEMOTICONMAXCOUNT - 1))/JBWEMOTICONMAXCOUNT
        //创建一个临时可变的二维空数组
        var tempList: [[JBWEmoticonModel]] = [[JBWEmoticonModel]]()
        
        //遍历
        for i in 0..<pageCount {
           var length = JBWEMOTICONMAXCOUNT
           let location = JBWEMOTICONMAXCOUNT * i
           
            //防止截取越界
            if location + length > emoticons.count {
                length = emoticons.count - location
            }
            
            //计算范围
            let range = NSRange(location: location, length: length)
            
            let arry = (emoticons as NSArray).subarray(with: range) as! [JBWEmoticonModel]
            
            //保存一维数组
            tempList.append(arry)
        }
        
        return tempList
    }
}


extension JBWEmoticonTools{
    //保存最近表情模型
    func saveRecentEmoticon(emoticonModel: JBWEmoticonModel){
        //遍历
        for (i, model) in recentEmoticons.enumerated() {
            //判断是否是emoji
            if emoticonModel.isEmoji {
                //去重
                if emoticonModel.code == model.code {
                    recentEmoticons.remove(at: i)
                }
            } else {
                //图片表情
                if emoticonModel.png == model.png {
                    recentEmoticons.remove(at: i)
                }
            }
        }
        //添加模型数组
        recentEmoticons.insert(emoticonModel, at: 0)
        
        //超过20个删除
        if recentEmoticons.count > 20 {
            recentEmoticons.removeLast()
        }
        
        //三维数组中的
        allEmoticons[0][0] = recentEmoticons
        //保存到本地沙盒
        NSKeyedArchiver.archiveRootObject(recentEmoticons, toFile: file)
    }
    
    //获取保存到沙盒中的数据
    func getRecentEmoticon() -> [JBWEmoticonModel] {
        guard let tempList = NSKeyedUnarchiver.unarchiveObject(withFile: file) as? [JBWEmoticonModel] else {
            return [JBWEmoticonModel]()
        }
        return tempList
    }
}
